package com.sky.service;

import com.sky.entity.Student;
import com.sky.mapper.StudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Random;

@Service
@Slf4j
public class StudentTaskService {
    @Autowired
    private StudentMapper studentMapper;

    private final Random random = new Random();

  //  @Scheduled(fixedRate = 1000) // 每隔5秒执行一次
    public void insertStudentData() {
        Student student = new Student();
        student.setName(getRandomName());
        student.setGender(random.nextInt(3)); // 0女 1男 2其他
        student.setAge(6 + random.nextInt(6)); // 6~11岁
        student.setGrade("一年级"); // 可扩展为随机年级
        student.setClassName("一班"); // 可扩展为随机班级
        student.setScoreMath(getRandomScore());
        student.setScoreEnglish(getRandomScore());
        student.setScoreChinese(getRandomScore());
        student.setStatus(0); // 默认正常状态
        student.setCreateTime(LocalDateTime.now());
        log.info("插入数据：{}", student);
      //  studentMapper.insertStudent(student);
    }

    private String getRandomName() {
        String[] names = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"};
        return names[random.nextInt(names.length)];
    }

    private Double getRandomScore() {
        return Math.round((50 + random.nextDouble() * 50) * 10) / 10.0; // 50~100 分，保留一位小数
    }
}
